<template>
    <!-- 入场管理-入场审批 -->
    <div class="empReward">
        <div class="top">
            <div class="search">
                <div class="list">
                    <!-- <DatePicker type="daterange" :clearable="false" placeholder="开始时间   -   结束时间" style="width: 200px"
                        v-model="value" @on-change="getTime"></DatePicker> -->
                    <el-date-picker
                        v-model="value"
                        type="datetimerange"
                        range-separator="至"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        @change="getTime"
                        :clearable="false"
                        format="yyyy-MM-dd HH:mm:ss"
                        value-format="yyyy-MM-dd HH:mm:ss">
                    </el-date-picker>
                </div>
                <div class="list" @click="onSerch">
                    <Button type="primary">搜索</Button>
                </div>
                <div class="list" @click="onReset">
                    <Button>重置</Button>
                </div>
            </div>
            <div class="down">
                <el-button type="primary" size="small" plain @click="onReset">刷新</el-button>
            </div>
        </div>
        <div class="bottom">
            <div class="table">
                <el-table :data="list" :height="tableHeight" :stripe="true" style="width: 100%">
                    <el-table-column prop="entryApply.id" label="ID" width="55"></el-table-column>
                    <el-table-column prop="entryApply.title" label="标题" min-width="150"></el-table-column>
                    <el-table-column prop="teamLeader.empName" label="姓名" width="100"></el-table-column>
                    <!-- <el-table-column prop="entryApply.idCard" label="身份证号" min-width="140"></el-table-column> -->
                    <el-table-column prop="entryPersons" label="入场人员" min-width="200">
                        <template slot-scope="scope">
                            <el-link :underline="false" v-for="(item,index) in scope.row.entryPersons" :key="index">
                                {{item.nickname + ', '}}
                            </el-link>
                        </template>
                    </el-table-column>
                    <el-table-column prop="entryApply.entryTime" label="入场时间" width="110"></el-table-column>
                    <el-table-column prop="entryApply.leaveTime" label="离场时间" width="110"></el-table-column>
                    <el-table-column prop="teamLeader.corpName" label="所属单位" min-width="180"></el-table-column>
                    <el-table-column prop="entryApply.filePath" label="附件文件" min-width="100">
                        <template slot-scope="scope">
                            <el-link v-if="scope.row.entryApply.filePath" target="_blank" :href="scope.row.entryApply.filePath" type="primary" >附件</el-link>
                            <el-link v-else type="info" :underline="false" >附件</el-link>
                        </template>
                    </el-table-column>
                    <el-table-column prop="entryApply.reason" label="事由" min-width="200"></el-table-column>
                    <el-table-column prop="entryApply.createTime" label="申请时间" width="110"></el-table-column>
                    <el-table-column prop="auditStatus" label="状态" width="150">
                        <template slot-scope="scope">
                            <el-tag :type="
                                    scope.row.entryApply.approvalStatus == 0 ? 'primary' :
                                    scope.row.entryApply.approvalStatus == 1 ? 'primary' :
                                	scope.row.entryApply.approvalStatus == 2 ? 'success' :
                                	scope.row.entryApply.approvalStatus == 3 ? 'danger' :
                                	scope.row.entryApply.approvalStatus == 4 ? 'info' : 'info'
                                " >
                                {{
                                	scope.row.entryApply.approvalStatus == 0 ? '待审批' :
                                	scope.row.entryApply.approvalStatus == 1 ? '审批中' :
                                	scope.row.entryApply.approvalStatus == 2 ? '已通过' :
                                	scope.row.entryApply.approvalStatus == 3 ? '已拒绝' :
                                	scope.row.entryApply.approvalStatus == 4 ? '审批结束' : '已结束'
                                }}
                            </el-tag>
                        </template>
                    </el-table-column>

                    <el-table-column label="操作" align="center" fixed="right" min-width="100">
                        <template slot-scope="scope">
                            <el-button type="text" size="small" @click="onView(scope.row, scope.$index)">查看</el-button><br />
                            <!--  2：通过 3：拒绝-->
                            <el-button type="text" size="small" @click="onReview(scope.row, 2)"
                                v-if="(scope.row.entryApply.approvalStatus == 1 || scope.row.entryApply.approvalStatus == 0) && scope.row.flag == true">
                                通过
                            </el-button>
                            <el-button type="text" size="small" @click="onReview(scope.row, 3)"
                                 v-if="(scope.row.entryApply.approvalStatus == 1 || scope.row.entryApply.approvalStatus == 0) && scope.row.flag == true">
                                 拒绝
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="clearfix"  >
                <div class="float-right">
                    <Page v-if="totalCount" style="margin: 20px 0;text-align: center;" :current.sync="current"
                        :total="totalCount" :page-size="pageSize" show-elevator show-total show-sizer
                        @on-change="pageChange" @on-page-size-change="pageSizeChange">
                    </Page>
                </div>
            </div>
        </div>

        <Modal v-model="deleteModal" title="删除" width="360px;" :mask-closable="false">
            <div style="margin:10px">确定要删除这条数据吗？</div>
            <div slot="footer">
                <el-button @click="cancel(2)" type="info" size="small" plain>取消</el-button>
                <el-button @click="detele" type="primary" size="small" plain v-preventReClick="1500">确定</el-button>
            </div>
        </Modal>

        <!-- Table -->
        <el-dialog title="入场审核" :visible.sync="dialogTableVisible" width="80%" top="8vh">
          <div class="bottom">
              <div class="table">
                  <el-table :data="gridData.approvalRecords" :height="tableHeight" :stripe="true" style="width: 100%">
                      <el-table-column prop="id" label="ID" width="55"></el-table-column>
                      <el-table-column prop="applicantUser.nickname" label="申请人"></el-table-column>
                      <el-table-column prop="approvalUser.nickname" label="审批人"></el-table-column>
                      <el-table-column prop="approvalTime" label="审批时间"></el-table-column>
                      <!-- <el-table-column prop="reason" label="事由" min-width="150"></el-table-column> -->
                      <el-table-column prop="auditStatus" label="状态">
                          <template slot-scope="scope">
                              <el-tag :type="
                                    scope.row.approvalResult == 0 ? 'primary' :
                                    scope.row.approvalResult == 1 ? 'primary' :
                                    scope.row.approvalResult == 2 ? 'success' :
                                    scope.row.approvalResult == 3 ? 'danger' :
                                    scope.row.approvalResult == 4 ? 'info' : 'info'
                                  " >
                                  {{
                                    scope.row.approvalResult == 0 ? '待审批' :
                                    scope.row.approvalResult == 1 ? '审批中' :
                                    scope.row.approvalResult == 2 ? '已通过' :
                                    scope.row.approvalResult == 3 ? '已拒绝' :
                                    scope.row.approvalResult == 4 ? '审批结束' : '已结束'
                                  }}
                              </el-tag>
                          </template>
                      </el-table-column>

                      <el-table-column label="操作" align="center" fixed="right" width="130">
                          <template slot-scope="scope">
                              <!--  0 ? '待审批' 1 ? '审批中' 2 ? '已通过' 3 ? '已拒绝' 4 ? '审批结束' flag：true 有 flase 没有 -->
                                <div v-if="(scope.row.approvalResult == 1 || scope.row.approvalResult == 0)
                                    && scope.row.flag == true && scope.row.approvalUser.id == userId">
                                    <el-button type="text" size="small" @click="onReview(gridData, 2)">通过</el-button>
                                    <el-button type="text" size="small" @click="onReview(gridData, 3)">拒绝</el-button>
                                </div>
                                <!-- <el-tag v-if="scope.row.approvalResult != 2 && scope.row.reviewDept" >
                                    {{scope.row.reviewDept}}
                                </el-tag> -->
                          </template>
                      </el-table-column>
                  </el-table>
              </div>
          </div>
        </el-dialog>
    </div>
</template>

<script>
    import {
        getEmpRewardList,
        getEmpRewardAdd,
        getEmpRewardDel,
        getAllPersonnelList,
        getSelectValue,
    } from "@/api/api.js";
    import {
        apiApplyWorkQueryReviewList,
        applyWorkEntryReview,
        apiApplyList
    } from "@/api/apifox.js";
    export default {
        data() {
            return {
                current: 1,
                pageSize: 10,
                pageNum: 1,
                totalCount: "",
                corpType: "",
                projectId: "",
                keyword: "",
                list: [],
                corpList: [],
                updateModal: false,
                isAdd: true,
                removeId: "",
                deleteModal: false,
                btnList: [],
                deptName: "",
                startDate: "",
                endDate: "",
                value: [],
                empList: [],
                empId: "",
                empName: "",
                date: "",
                tableHeight: window.innerHeight - 300,
                itemList: [], //改动 下拉值
                itemId: "",
                radio: "",
                description: "",
                labelByValue: {},

                dialogTableVisible: false, // 列表弹出层
                gridIndex: null, // 弹出层数据索引
                gridData: [], // 弹出层列表
                userId: null, // 用户id
            };
        },
        computed: {
            porjectObject() {
                return this.$store.state.porjectObject;
            }
        },
        watch: {
            porjectObject(val, oldVal) {
                if (val.deptId != oldVal.deptId) {
                    this.projectId = val.deptId;
                    this.deptName = val.name;
                    this.pageNum = 1;
                    this.current = 1;
                    this.keyword = "";
                    this.value = [];
                    this.getAllList();
                    this.getEmpList();
                }
            }
        },

        created() {
            // this.selectValue(); //改动 初次加载下拉数据
        },
        mounted() {
            this.projectId = this.$store.state.porjectObject.deptId;
            this.deptName = this.$store.state.porjectObject.name;
            this.userId = localStorage.getItem('userId')
            console.log('123456', this.userId)
            this.getAllList();
            this.getEmpList();
            this.btnList = JSON.parse(localStorage.getItem("arr"));
        },
        methods: {
            onReview(row, e) {
                this.$confirm(e == 2 ? '是否确定通过此入场申请通过！' : '是否确定拒绝此入场申请通过！', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                    center: true
                }).then(() => {
                    let currentApprovalRecord;
                    for (var index = 0; index < row.approvalRecords.length; index++) {
                    	var element = row.approvalRecords[index];
                    	if(element.approverId == this.userId){
                    		currentApprovalRecord = element
                    	}
                    }
                    let param = {
                    	id: currentApprovalRecord.id,
                    	approvalResult: e
                    };
                    console.log(param);
                    // return
                    this.applyWorkEntryReview(param);
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消审核'
                    });
                });
            },
            onView(row, index) {
                console.log(index);
                this.gridIndex = null;
                this.gridData = [];

                this.gridIndex = index;
                this.gridData = row;
                let approvalRecords = [];

                row.approvalRecords.forEach((item, index) => {
                    /* 0: '待审批' / 1: '审批中' / 2: '已通过' / 3: '已拒绝' / 4: '审批结束' / */
                    if (item.approvalResult == 2 || item.approvalResult == 3 || item.approvalResult == 4) {
                        approvalRecords.push(item);
                    }
                });
                this.gridData.approvalRecords = approvalRecords;
                this.dialogTableVisible = true;
                /* apiApplyWorkQueryReviewList({
                    id: row.entryApply.id,
                    isReview: 1 // (1:入场,2:计划)
                }).then(res => {
                    if (res.code == 200) {
                        this.gridData = res.data;
                        this.dialogTableVisible = true;
                        // this.totalCount = res.data.total;
                    } else {
                        this.$Message.warning(res.msg);
                    }
                }).catch(error => {
                    console.log(error);
                }); */
            },
            applyWorkEntryReview(param) {
                applyWorkEntryReview(param).then(res => {
                    if (res.code == 200) {
                        this.$message({
                            type: 'success',
                            message: param.approvalResult == 2 ? '已通过入场审核' : '已拒绝入场审核'
                        });
                        this.dialogTableVisible = false;
                        this.onReset();
                    } else {
                        this.$Message.warning(res.msg);
                    }
                });
            },
            //获取所有人员
            getEmpList() {
                let params = {
                    deptId: this.projectId
                };
                getAllPersonnelList(params)
                    .then(res => {
                        if (res.code == 200) {
                            this.empList = res.data.list;
                        } else {
                            this.$Message.warning(res.msg);
                        }
                    })
                    .catch(error => {
                        console.log(error);
                    });
            },
            getAllList() {
                /* let params = {
                    pageSize: this.pageSize, // 分页大小
                    pageNum: this.pageNum, // 分页页码
                    keyword: this.keyword,
                    deptId: this.projectId,
                    startTiem: this.startDate, // 开始时间
                    endTiem: this.endDate, // 结束时间
                    createUserId: [], // 创建人Id
                    isExit: null, // 是否离场 1到场 2离场
                    id: null,
                    pidList: [],
                };
                apiApplyList(params).then(res => {
                    console.log('apiApplyList 请求结果', res)
                    if (res.code == 200) {
                        this.list = res.data.list;
                        this.totalCount = res.data.total;
                    } else {
                        this.$Message.warning(res.msg);
                    }
                }).catch(error => {
                    console.log(error);
                }); */
                let params = {
                    entryTimeBegin: this.startDate,
                    entryTimeEnd: this.endDate,
                    pageSize: this.pageSize, // 分页大小
                    pageNum: this.pageNum, // 分页页码
                };
                apiApplyWorkQueryReviewList(params).then(res => {
                    if (res.code == 200) {
                        this.list = res.data.list;
                        this.totalCount = res.data.total;
                    } else {
                        this.$Message.warning(res.msg);
                    }
                }).catch(error => {
                    console.log(error);
                });
            },
            getTime(value) {
                if (value === null) {
                    console.log("点击了清空按钮，日期已清空");
                    this.startDate = '';
                    this.endDate = '';
                    // 处理清空后的逻辑
                } else {
                    console.log("选择了日期：", value);
                    this.startDate = value[0];
                    this.endDate = value[1];
                }
                // this.pageNum = 1;
                // this.current = 1;
                // this.getAllList();
            },
            // onSearch() {
            //     this.pageNum = 1;
            //     this.current = 1;
            //     this.getAllList();
            // },

            onSerch() {
                this.pageNum = 1;
                this.current = 1;
                this.getAllList();
            },
            onReset() {
                this.value = []
                this.startDate = null
                this.endDate = null
                this.keyword = null
                this.pageNum = 1;
                this.current = 1;
                this.getAllList();
                this.getEmpList();
            },
            pageChange(index) {
                this.pageNum = index;
                this.getAllList();
            },
            pageSizeChange(index) {
                this.pageSize = index;
                this.getAllList();
            },
            updateRow() {
                this.updateModal = true;
                this.empId = "";
                this.empName = "";
                this.date = "";
                this.radio = 1;
                this.itemId = "";
                this.description = "";
            },
            getEmpType(index) {
                this.empId = index;
                this.empList.forEach(i => {
                    if (index == i.empId) {
                        this.empName = i.empName;
                    }
                });
            },
            getDate(value) {
                this.date = value;
            },
            submitAll() {
                if (this.empId == "") {
                    this.$Modal.warning({
                        title: "您有必填字段",
                        content: "请选择人员"
                    });
                    return;
                }
                if (this.date == "") {
                    this.$Modal.warning({
                        title: "您有必填字段",
                        content: "请选择发生时间"
                    });
                    return;
                }
                if (this.itemId == "") {
                    this.$Modal.warning({
                        title: "您有必填字段",
                        content: "请选择奖惩分类"
                    });
                    return;
                }
                if (this.description == "") {
                    this.$Modal.warning({
                        title: "您有必填字段",
                        content: "请输入描述"
                    });
                    return;
                }
                let params = {
                    empId: this.empId,
                    empName: this.empName,
                    type: this.radio,
                    item: this.itemId,
                    description: this.description,
                    date: this.date
                };
                getEmpRewardAdd(params)
                    .then(res => {
                        if (res.code == 200) {
                            this.$Message.success("操作成功");
                            this.updateModal = false;
                            this.getAllList();
                        } else {
                            this.$Message.warning(res.msg);
                        }
                    })
                    .catch(error => {
                        console.log(error);
                    });
            },
            remove(index, rows) {
                this.removeId = rows[index].id;
                this.deleteModal = true;
            },
            detele() {
                getEmpRewardDel(this.removeId)
                    .then(res => {
                        if (res.code == 200) {
                            this.$Message.success("操作成功");
                            this.getAllList();
                            this.deleteModal = false;
                        } else {
                            this.$Message.warning(res.msg);
                        }
                    })
                    .catch(error => {
                        console.log(error);
                    });
            },

            cancel(type) {
                if (type == 1) {
                    this.updateModal = false;
                } else if (type == 2) {
                    this.deleteModal = false;
                }
            },
            //改动
            selectValue() {
                /* let params = {
                    code: "reward_type"
                };
                const _this = this;
                getSelectValue(params)
                    .then(res => {
                        if (res.code == 200) {
                            this.itemList = res.data.childList;
                            let province_list = {};
                            for (let i = 0; i < this.itemList.length; i++) {
                                province_list[this.itemList[i].code] = this.itemList[i].name;
                            }
                            this.labelByValue = province_list;
                        }
                    })
                    .catch(error => {
                        console.log(error);
                    }); */
            },
        },
    };
</script>

<style scoped lang="less">
    // @import "./../../assets/style/emp_common.less";
    @import "./../../assets/style/themeBase.less";

    .empReward {
        width: 100%;
        height: 100%;
        padding: 10px;
        box-sizing: border-box;
        background: #f5f5f5;

        .top {
            width: 100%;
            height: 72px;
            background: #fff;
            border-radius: 10px;
            display: flex;
            justify-content: space-between;
            align-items: center;

            .search {
                width: auto;
                margin-left: 20px;
                padding-top: 20px;
                display: flex;
                justify-content: flex-start;
                align-items: center;

                .list {
                    display: flex;
                    justify-content: flex-start;
                    align-items: center;
                    padding-bottom: 20px;
                    margin-right: 20px;

                    span {
                        width: 70px;
                    }
                }
            }

            .down {
                width: auto;
                margin-right: 20px;
            }
        }

        .bottom {
            width: 100%;
            padding: 20px;
            margin-top: 10px;
            box-sizing: border-box;
            height: calc(100% - 82px);
            background: #fff;
            border-radius: 10px;

            .down {
                width: 98%;
                margin: 0 auto;
                margin-top: 20px;
            }

            .msg {
                margin-bottom: 20px;
                display: flex;
                flex-wrap: wrap;

                .list {
                    display: flex;
                    justify-content: flex-start;
                    align-items: center;
                    margin-right: 24px;

                    p {
                        margin-right: 10px;
                        font-size: 16px;
                        color: #000;

                        &:nth-child(2) {
                            color: red;
                        }
                    }
                }
            }

            .updateCorp {
                cursor: pointer;
                margin-left: 5px;
                padding: 2px 4px;
                font-size: 12px;
                font-weight: 400;
                line-height: 1;
                color: #ffffff;
                border-radius: 3px;
                background-color: var(--themeColor);
            }
        }
    }

    .item {
        width: 400px;
        display: flex;
        margin: 0 auto;
        justify-content: space-between;
        align-items: center;
        margin-top: 10px;
        position: relative;
        margin-bottom: 20px;
        padding-left: 16px;
        box-sizing: border-box;

        .radio {
            width: 300px;
            display: flex;
            justify-content: center;
        }

        .tip {
            position: absolute;
            top: 8px;
            left: 0;
            color: red;
            font-size: 16px;
        }
    }

    // /deep/.ivu-icon-ios-close{
    //     color: #fff;
    //     &:hover{
    //         color: #fff;
    //     }
    // }



    /deep/.ivu-input-group {
        border-radius: 20px;
    }

    /deep/.ivu-input-group .ivu-input {
        border-radius: 20px 0 0 20px;
        border-color: #EBF2F8;
        background: #EBF2F8;
    }

    /deep/.ivu-input-group-append {
        border-radius: 0 20px 20px 0;
    }

    /deep/.el-tag.el-tag--success {
        background-color: #E7F8F2;
        border-color: #28D79C;
        color: #3FCF77;
    }

    /deep/.el-tag {
        border-radius: 2px;
    }

    /deep/.ivu-select-selection {
        border-radius: 2px;
    }

    /deep/.el-tag.el-tag--info {
        background-color: rgba(255, 72, 72, 0.1);
        border-radius: 2px;
        border-color: #FF4848;
        color: #FF4848;
    }

    .el-table {
        ::-webkit-scrollbar {
            height: 10px;
            /* 横向滚动条 高度 */
        }

        ::-webkit-scrollbar-thumb {
            // 滚动条上的滚动滑块
            // background: #000 !important;
            height: 10px;
        }
    }
</style>
